package org.example.demo.mapper;

import org.example.demo.entity.District;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class DistrictDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public int add(District district){
        return jdbcTemplate.update("insert into district(name) values (?)",district.getName());
    }

    public int del(Integer id){
        return jdbcTemplate.update("delete from district where id=?", id);
    }

    public int update(District district){
        return jdbcTemplate.update("update district set name = ? where id = ?",district.getName(),district.getId());
    }
    public District get(Integer id){
        return jdbcTemplate.queryForObject("select * from district where id = ?", BeanPropertyRowMapper.newInstance(District.class), id);
    }

    public List<District> getAll(){
        return jdbcTemplate.query("select * from district",BeanPropertyRowMapper.newInstance(District.class));
    }
}
